import { Router, Request, Response, RequestHandler } from 'express';
import { recognizeImageBaidu } from '../services/openaiService';

const router = Router();

const guessHandler: RequestHandler = async (req, res) => {
  const { image } = req.body;

  if (!image || typeof image !== 'string' || !image.startsWith('data:image/')) {
    res.status(400).json({ message: '无效的图像数据格式' });
    return;
  }

  try {
    const guess = await recognizeImageBaidu(image);

    console.log(`AI (Baidu) 猜测结果: ${guess}`);
    res.status(200).json({ guess });

  } catch (error) {
    console.error('处理猜测请求时出错 (Baidu):', error);
    const message = error instanceof Error ? error.message : '服务器内部错误';
    res.status(500).json({ message });
  }
};

router.post('/', guessHandler);

export default router;
